package jh.com.jhappbf.utils;

import java.util.Locale;

/**
 * @author Administrator
 */
public class MoneyNumToChinese {
	private static final char [] ChineseNum ={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
	private static final char [] ChineseUnit={'里','分','角','元','拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟'};

	/**
	 * 返回关于钱的中文式大写数字,支仅持到亿
	 * */
	public static String arabNumToChineseRMB(int moneyNum){
		StringBuilder res= new StringBuilder();
		int i=3;
		if(moneyNum==0) {
			return "零元";
		}
		while(moneyNum>0){
			res.insert(0, ChineseUnit[i++]);
			res.insert(0, ChineseNum[moneyNum % 10]);
			moneyNum/=10;
		}
		return res.toString().replaceAll("零[拾佰仟]", "零")
				.replaceAll("零+亿", "亿").replaceAll("零+万", "万")
				.replaceAll("零+元", "元").replaceAll("零+", "零");

	}

	/**
	 * 返回关于钱的中文式大写数字,仅支持到亿
	 *
	 * */
	public  static String arabNumToChineseRMB(String moneyNum) throws Exception{
		StringBuilder res= new StringBuilder();
		int i=3;
		int len=moneyNum.length();
		if(len>12){
			throw new Exception("Number too large!");
		}
		if("0".equals(moneyNum)) {
			return "零元";
		}
		//System.out.println(moneyNum);
		for(len--;len>=0;len--){
			res.insert(0, ChineseUnit[i++]);
			int num=Integer.parseInt(moneyNum.charAt(len)+"");
			res.insert(0, ChineseNum[num]);
		}
		return res.toString().replaceAll("零[拾佰仟]", "零")
				.replaceAll("零+亿", "亿").replaceAll("零+万", "万")
				.replaceAll("零+元", "元").replaceAll("零+", "零");

	}
	/**
	 * 整数位支持12位,到仟亿
	 * 支持到小数点后3位,如果大于3位,那么会四舍五入到3位
	 *
	 * */
	public static String arabNumToChineseRMB(double moneyNum) throws Exception{
		StringBuilder res= new StringBuilder();
		String money=String.format(Locale.CHINESE,"%.3f",moneyNum);
		//System.out.println(money);
		int i=0;
		if(moneyNum==0.0) {
			return "零元";
		}
		String integer = money.split("\\.")[0];
		int deci=Integer.parseInt(money.split("\\.")[1].substring(0, 3));
		while(deci>0){
			res.insert(0, ChineseUnit[i++]);
			res.insert(0, ChineseNum[deci % 10]);
			deci/=10;
		}
		res = new StringBuilder(res.toString().replaceAll("零[里分角]", "零"));
		if(i<3) {
			res.insert(0, "零");
		}
		res = new StringBuilder(res.toString().replaceAll("零+", "零"));
		if(res.toString().endsWith("零")) {
			res = new StringBuilder(res.substring(0, res.length() - 1));
		}
		return arabNumToChineseRMB(integer)+res;
	}
}
